import urllib, urllib2, json

URL_ACCESS_TOKEN = 'https://api.weixin.qq.com/cgi-bin/token'
URL_USER_INFO = 'https://api.weixin.qq.com/cgi-bin/user/info'

class MediaPlatform:

  def __init__(self, app_id, app_secret):
    self.app_id  = app_id
    self.app_secret = app_secret

  def request_access_token(self):
    param = {}
    param['grant_type'] = 'client_credential'
    param['appid'] = self.app_id
    param['secret'] = self.app_secret
    full_url = URL_ACCESS_TOKEN + '?' + urllib.urlencode(param)
    response = urllib2.urlopen(full_url)
    return json.loads(response.read())['access_token']

  def request_user_info(self, access_token, openid):
    param = {}
    param['access_token'] = access_token
    param['openid'] = openid
    param['lang'] = 'zh_CN'
    full_url = URL_USER_INFO + '?' + urllib.urlencode(param)
    response = urllib2.urlopen(full_url)
    return response.read()

if __name__ == '__main__':
  pass
  #mp = MediaPlatform('xxxx', 'xxx').request_access_token()
  #print mp.request_user_info(token, 'openid')
